package org.jet.emall.rbac.service;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.jet.emall.common.constant.RoleName;
import org.jet.emall.common.util.RegUtils;
import org.jet.emall.rbac.po.AdminOperation;
import org.jet.emall.rbac.po.AdminUrlPrivilegeRoleView;
import org.jet.emall.rbac.service.mp.IAdminOperationService;
import org.jet.emall.rbac.service.mp.IAdminUrlPrivilegeRoleViewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 加载动态权限
 *
 * @author xiaozai
 * @version 1.0
 * @date 2020-04-09 13:19
 */
@Service
public class CustomAdminUrlPrivilegeRoleViewServiceImpl implements CustomAdminUrlPrivilegeRoleViewService {

    @Autowired
    IAdminUrlPrivilegeRoleViewService adminUrlPrivilegeRoleViewService;
    @Autowired
    IAdminOperationService adminOperationService;


    @Override
    public Map<String, Map<String, Set<String>>> loadPrivilegesMap() {

        List<AdminUrlPrivilegeRoleView> list = adminUrlPrivilegeRoleViewService.list();
        System.out.println("list="+list);
        Map<String, Map<String, Set<String>>> result = new HashMap<>();
        list.forEach(adminUrlPrivilegeRoleView -> {
            String roleName = adminUrlPrivilegeRoleView.getRoleName();
            Map<String, Set<String>> stringSetMap = result.get(roleName);
            if (stringSetMap == null) {
                stringSetMap = new HashMap<>();
                result.put(roleName, stringSetMap);
            }
            Set<String> stringSet = stringSetMap.get(adminUrlPrivilegeRoleView.getMethod());
            if (stringSet == null) {
                stringSet = new HashSet<>(16);
                stringSetMap.put(adminUrlPrivilegeRoleView.getMethod(), stringSet);
            }
            stringSet.add(RegUtils.handle(adminUrlPrivilegeRoleView.getUrl()));
        });

        //获取所有不属于菜单的url,因为不属于菜单，而角色的分配权限是以一级菜单展开的树形权限，所以不包含这些没有菜单id的权限所以要单独加载
        Map<String, Set<String>> allRole = getUrlsForBaseRole();
        if (allRole != null) {
            result.put(RoleName.BASE_ROLE_NAME, allRole);
        }
        return result;
    }

    private Map<String, Set<String>> getUrlsForBaseRole() {

        Wrapper<AdminOperation> wrapper = new QueryWrapper<>();
        ((QueryWrapper<AdminOperation>) wrapper).isNull("menu_id");
        List<AdminOperation> list = adminOperationService.list(wrapper);
        Map<String, Set<String>> result = new HashMap<>();
        list.forEach(adminOperation -> {
            Set<String> stringSet = result.get(adminOperation.getMethod());
            if (stringSet == null) {
                stringSet = new HashSet<>();
                result.put(adminOperation.getMethod(), stringSet);
            }
            stringSet.add(RegUtils.handle(adminOperation.getUrl()));
        });
        if(result.isEmpty()){
            return null;
        }
        return result;
    }
}
